package com.sunnada.weixin.util.weixinPay;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 各种数据类型的转化
 * @author apple
 *
 */
public final class ConvertionUtil {
	
	
	public static byte[] StringToUTF8BytesPadding(String str)
	{
		byte[] copyBts = null;
		try {
			byte[] bts = str.getBytes("utf-8");
			int iMod = bts.length % 8;
			if(0 == iMod) {
				copyBts = bts;
			} else {
				copyBts = new byte[bts.length + 8 - iMod];
				System.arraycopy(bts, 0, copyBts, 0, bts.length);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return copyBts;
		
	}
	
	public static byte[] StringToUTF8Bytes(String str)
	{
		try {
			return str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	
	//字节转化为字符串
	public static String BytesToHexString(byte[] bytes, boolean upperCase)
	{
		StringBuffer s = new StringBuffer();
		for (int i = 0; i< bytes.length; i++)
		{
			 int v = bytes[i] & 0xFF;  
		        String hv = Integer.toHexString(v);  
		        if (hv.length() < 2) {  
		            s.append(0);  
		        }  
		        s.append(hv);
		}
		
		return upperCase ? s.toString().toUpperCase() : s.toString();
	}
	
	public static byte[] HexStringToBytes(String str)
	{
		if (str == null || str.equals("")) {  
	        return null;  
	    }  
		str = str.toUpperCase();  
	    int length = str.length() / 2;  
	    char[] hexChars = str.toCharArray();  
	    byte[] d = new byte[length];  
	    for (int i = 0; i < length; i++) {  
	        int pos = i * 2;  
	        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
	    }  
	    return d;  
	}
	
	
	 private static byte charToByte(char c) {  
		    return (byte) "0123456789ABCDEF".indexOf(c);  
	 }
	 
	 //字符串转化为日期
	 public static Date StringToDate(String date, String format)
	 {
		 if (format == null)
			 format = "yyyy-MM-DD HH:mm:ss";
		 
		 try {
			return (new SimpleDateFormat(format, Locale.CHINA)).parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 return null;
	 }
	 
	 public static String DateToString(Date date, String format)
	 {
		 if (format == null)
			 format = "yyyy-MM-dd HH:mm:ss";
		 
		 return (new SimpleDateFormat(format, Locale.CHINA)).format(date);
	 }
	 
	 
	 /*
	  * 计算当前日期到指定日期之间的间隔时间字符串，这里表示的是中文方式
	  * 1分钟内	一分钟前
		1分钟--5分钟	5分钟前
		5分钟--10分钟	10分钟前
		10分钟--30分钟	半小时前
		30分钟--1小时	1小时前
		1小时--2小时	2小时前
		2小时--3小时	3小时前	　
		夸1天	昨天
  		2天	1天前
 		3天以上	X月 X 日
	  */
	 @SuppressWarnings("deprecation")
	public static String DateIntevalString(Date date)
	 {
		 //取当天时间
		 Date curDate = new Date();
		 long distance = curDate.getTime() - date.getTime();
		 
		 //表明是未来，直接显示日期
		 if (distance < 0)
			 return DateToString(date, null);
		 else if (distance < 1000 * 60)
			 return "一分钟前";
		 else if (distance < 1000 * 60 * 5)
			 return "五分钟前";
		 else if (distance < 1000 * 60 * 10)
			 return "十分钟前";
		 else if (distance < 1000 * 60 * 30)
			 return "半小时前";
		 else if (distance < 1000 * 60 * 60)
			 return "一小时前";
		 else if (distance < 1000 * 60 * 60 * 2)
			 return "两小时前";
		 else if (distance < 1000 * 60 * 60 * 3)
			 return "三小时前";
		 else if (distance < 1000 * 60 * 60 * 4)
			 return "四小时前";
		 else if (distance < 1000 * 60 * 60 * 5)
			 return "五小时前";
		 else if (distance < 1000 * 60 * 60 * 6)
			 return "六小时前";
		 else if (distance < 1000 * 60 * 60 * 7)
			 return "七小时前";
		 else if (distance < 1000 * 60 * 60 * 8)
			 return "八小时前";
		 else if (distance < 1000 * 60 * 60 * 9)
			 return "九小时前";
		 else if (distance < 1000 * 60 * 60 * 10)
			 return "十小时前";
		 else if (distance < 1000 * 60 * 60 * 24)
		 {
			 if (curDate.getDay() - date.getDay() == 0)
				 return "半天前";
			 else
				 return "昨天";
		 }
		 else if (curDate.getYear() == date.getYear() && 
				 curDate.getMonth() == date.getMonth()  && 
				 curDate.getDay() - date.getDay() <= 2)
			 return "前天";
		 else
		 {
			 if (curDate.getYear() != date.getYear())
				 return DateToString(date, "yyyy-MM-dd");
			 else
				 return DateToString(date, "MM-dd");
		 }
	 }

}
